<?php

namespace app\admin\controller\speech;

use app\common\controller\Backend;
use think\Db;
use think\Request;

/**
 * 话术评价
 *
 * @icon fa fa-circle-o
 */
class Grade extends Backend
{

    /**
     * SpeechGrade模型对象
     * @var \app\admin\model\SpeechGrade
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('SpeechGrade');
        $this->view->assign("routernumList", $this->model->getRouternumList());
        $this->view->assign("faqnumList", $this->model->getFaqnumList());
        $speech_id = $this->request->get('speech_id');
        if (!is_null($speech_id)) {
            $this->init($speech_id);
        }
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $params['speech_id'] = $this->request->get('speech_id');
                    $params['admin_id'] = $this->auth->id;
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }


    public function init($id)
    {
        $admin_id = Db::name('speech')->where('id', $id)->value('admin_id');
        if (!is_null($admin_id)) {
            $data_a = ['name' => 'A类客户', 'speech_id' => $id, 'admin_id' => $admin_id, 'calltime' => 60, 'routernum' => 0, 'faqnum' => 3];
            if (Db::name('speech_grade')->where('speech_id', $id)->where('name', 'A类客户')->count() == 0) {
                Db::name('speech_grade')->insert($data_a);
            }

            $data_b = ['name' => 'B类客户', 'speech_id' => $id, 'admin_id' => $admin_id, 'calltime' => 30, 'routernum' => 0, 'faqnum' => 1];
            if (Db::name('speech_grade')->where('speech_id', $id)->where('name', 'B类客户')->count() == 0) {
                Db::name('speech_grade')->insert($data_b);
            }

            $data_c = ['name' => 'C类客户', 'speech_id' => $id, 'admin_id' => $admin_id, 'calltime' => 20, 'routernum' => 0, 'faqnum' => 0];
            if (Db::name('speech_grade')->where('speech_id', $id)->where('name', 'C类客户')->count() == 0) {
                Db::name('speech_grade')->insert($data_c);
            }

            $data_d = ['name' => 'D类客户', 'speech_id' => $id, 'admin_id' => $admin_id, 'calltime' => 0, 'routernum' => 0, 'faqnum' => 0];
            if (Db::name('speech_grade')->where('speech_id', $id)->where('name', 'D类客户')->count() == 0) {
                Db::name('speech_grade')->insert($data_d);
            }
        }else{
            return null;
        }
    }

    /**
     * 查看
     */
    public function index()
    {
        $speech_id = $this->request->get('speech_id');
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $where2['speech_id'] = ['=', $speech_id];
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['admin', 'speech'])
                ->where($where)
                ->where($where2)
                ->order($sort, $order)
                ->count();

            $order = 'asc';

            $list = $this->model
                ->with(['admin', 'speech'])
                ->where($where)
                ->where($where2)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach ($list as $row) {


            }
            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list, "speech_id" => $speech_id);

            return json($result);
        }
        return $this->view->fetch();
    }

}
